package interfazeak;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.mysql.jdbc.V1toV2StatementInterceptorAdapter;
import com.toedter.calendar.JCalendar;

import db.BotikaKud;
import db.UpelaKud;

public class Tratatu extends JDialog {

	private Upategia guraso;
	private final JPanel contentPanel = new JPanel();
	private JLabel data;
	private JCalendar cal;
	private JTextField field;
	private JButton gordebtn;
	private JButton atzera;
	private JLabel mezua;

	public static void main(String[] args) {
		try {
			final Boolean MODALA = true;
			int rs  = 0;
			Tratatu dialog = new Tratatu(new Upategia(), MODALA, rs);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Tratatu(Upategia guraso, Boolean mODALA, final int upelaz) {
		

		this.guraso = guraso;

		setBounds(120, 150, 748, 451);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		setTitle("TRATATU");
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		data = new JLabel("Data:");
		data.setBounds(270, 130, 70, 15);
		contentPanel.add(data);

		cal = new JCalendar();
		cal.setBounds(270, 160, 240, 200);
		contentPanel.add(cal);

		mezua = new JLabel ("Tratamendua:");
		mezua.setBounds(25, 30, 100, 20);
		contentPanel.add(mezua);
		
		field = new JTextField();
		field.setBounds(25, 50, 600, 40);
		contentPanel.add(field);

		gordebtn = new JButton("GORDE");
		gordebtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				String idatzi = field.getText();
				if (idatzi.equals("")) {
					JOptionPane.showMessageDialog(Tratatu.this, "Hutsik dago!");
				} else {
					int konfirmatu = JOptionPane.showConfirmDialog(
							Tratatu.this, "Tratamendua gehitu nahi duzu?");

					if (JOptionPane.OK_OPTION == konfirmatu) {

						String eguna = Tratatu.this.lortueguna();
						// upelaz-etaz lortuko dugu idUpelatzea. bakoitzeko
						// txertatu Tratamenduan idUpelatzea, data,tratamendua

						Tratatu.this.txertatutratatamendua(upelaz, idatzi, eguna);
						field.setText(null);
						Tratatu.this.dispose();

					}
					else if (JOptionPane.NO_OPTION == konfirmatu){
						Tratatu.this.dispose();
					}
				}
			}
		});

		gordebtn.setBounds(400, 390, 117, 25);
		contentPanel.add(gordebtn);

		atzera = new JButton("UPATEGIA");
		atzera.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				Tratatu.this.dispose();
			}
		});
		atzera.setBounds(200, 390, 117, 25);
		contentPanel.add(atzera);
	}

	protected void txertatutratatamendua(int upelaz, String idatzi,
			String eguna) {
		
		UpelaKud.txertatutratamendua(upelaz, idatzi, eguna);

	}

	protected String lortueguna() {

		String año = Integer.toString(cal.getCalendar().get(
				java.util.Calendar.YEAR));
		String mes = Integer.toString(cal.getCalendar().get(
				java.util.Calendar.MONTH) + 1);
		String dia = Integer.toString(cal.getCalendar().get(
				java.util.Calendar.DATE));

		String eguna = (dia + "-" + mes + "-" + año);
		return eguna;
	}

}
